package cn.vw.hi.user.persistence;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.vw.hi.commen.tools.Page;
import cn.vw.hi.user.model.WebsiteColumns;

public class WebsiteConfigerDaoImpl extends HibernateDaoSupport implements
		WebsiteConfigerDao {
	// 添加网站栏目
	public boolean addWebsiteColumn(final WebsiteColumns websiteColumns) {
		try
		{
			/*WebsiteColumns column=(WebsiteColumns)this.getHibernateTemplate().execute(new HibernateCallback(){

				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Criteria criteria=session.createCriteria(WebsiteColumns.class);
					criteria.add(Expression.eq("url", websiteColumns.getUrl()));
					return criteria.uniqueResult();
				}
			});*/
			this.getHibernateTemplate().save(websiteColumns);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//获得网站列表
	public List websiteColumnList(final Page pageObj) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(WebsiteColumns.class);
			 
			 if(pageObj!=null)
			 {
				 int rowCount=	(Integer)criteria.setProjection(Projections.count("id")).uniqueResult();
			     pageObj.setRowCount(rowCount);
	             pageObj.setTotalPage(rowCount);
				 criteria.setProjection(null);
				 criteria.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				 criteria.setMaxResults(pageObj.getPageSize());
			 }
				return criteria.list();
			}
			
		});
		return list;
	}
   //修改网站栏目
	public boolean modifyWebsiteColumn(final WebsiteColumns websiteColumns) {
		try
		{
			/*WebsiteColumns column=(WebsiteColumns)this.getHibernateTemplate().execute(new HibernateCallback(){

				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Criteria criteria=session.createCriteria(WebsiteColumns.class);
					criteria.add(Expression.eq("url", websiteColumns.getUrl()));
					criteria.add(Expression.not(Expression.eq("id", websiteColumns.getId())));
					return criteria.uniqueResult();
				}
			});*/
			this.getHibernateTemplate().update(websiteColumns);
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//获得栏目
	public WebsiteColumns findWebsiteColumn(Integer id) {
		return (WebsiteColumns)this.getHibernateTemplate().get(WebsiteColumns.class, id);
	}

}
